<template>
  <div class>
    <el-card class="btns">
      <el-button size="medium" type="primary" @click="submitForm('ruleForm')">查询</el-button>
      <el-button size="medium" @click="resetForm('ruleForm')">重置</el-button>
      <el-button size="medium" type="primary" icon="el-icon-plus" @click="showDialog">添加</el-button>
    </el-card>
    <el-card style="position:relative;">
      <el-form
        :style="style"
        :model="ruleForm"
        ref="ruleForm"
        inline
        label-width="100px"
        class="demo-ruleForm">
        <div class="icon-arrow" v-if="folding">
          <i @click="offForm=!offForm"
            :title="offForm?'展开表单':'收起表单'"
            :class="offForm?'el-icon-arrow-down':'el-icon-arrow-up'"></i>
        </div>
			                	<el-form-item label="调度任务名"  prop="jobName">
		                	<el-input v-model="ruleForm.jobName"></el-input>
		              </el-form-item>
	                	<el-form-item label="调度任务描述"  prop="jobDesc">
		                	<el-input v-model="ruleForm.jobDesc"></el-input>
		              </el-form-item>
	                	<el-form-item label="cron表达式"  prop="jobCron">
		                	<el-input v-model="ruleForm.jobCron"></el-input>
		              </el-form-item>
	                	<el-form-item label="调度任务类型"  prop="jobType">
		                	<el-input v-model="ruleForm.jobType"></el-input>
		              </el-form-item>
	                	<el-form-item label="任务来源ID"  prop="sourceId">
		                	<el-input v-model="ruleForm.sourceId"></el-input>
		              </el-form-item>
	                	<el-form-item label="调度任务状态"  prop="jobState">
		                	<el-input v-model="ruleForm.jobState"></el-input>
		              </el-form-item>
	                	<el-form-item label="JOB类路径"  prop="classPath">
		                	<el-input v-model="ruleForm.classPath"></el-input>
		              </el-form-item>
	                	<el-form-item label="JSON参数"  prop="jobData">
		                	<el-input v-model="ruleForm.jobData"></el-input>
		              </el-form-item>
	                <el-form-item label="创建人姓名"  prop="createName">
		                	<el-input v-model="ruleForm.createName"></el-input>
		              </el-form-item>
	                <!-- <el-form-item label="修改人姓名"  prop="updateName">
		                	<el-input v-model="ruleForm.updateName"></el-input>
		              </el-form-item> -->
      </el-form>
    </el-card>
    <el-table stripe :data="tableData" border style="width: 100%">
    		<!-- <el-table-column type="index" width="50px" align="center" fixed="left" label="序号"></el-table-column> -->
      <el-table-column align="center" label="调度任务名" width="100px">
        <template slot-scope="scope">
          <div :title="scope.row.jobName" class="overFlow">{{scope.row.jobName}}</div>
        </template>
	    </el-table-column>
      <el-table-column align="center" label="调度任务描述" width="120px">
        <template slot-scope="scope">
          <div :title="scope.row.jobDesc" class="overFlow">{{scope.row.jobDesc}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="cron表达式" width="120px">
        <template slot-scope="scope">
          <div :title="scope.row.jobCron" class="overFlow">{{scope.row.jobCron}}</div>
        </template>
	    </el-table-column>
      <el-table-column align="center" label="任务类型" width="110px">
        <template slot-scope="scope">
          <div :title="scope.row.jobType" class="overFlow">{{getLabelByCode(scope.row.jobType, jobTypes)}}</div>
        </template>
      </el-table-column>
    
        <el-table-column align="center" label="任务状态">
        <template slot-scope="scope">
              <span>{{getLabelByCode(scope.row.jobState, jobStates)}}</span>
        </template>
        </el-table-column>
            <el-table-column align="center" label="JOB类路径" width="110px">
        <template slot-scope="scope">
          <div :title="scope.row.classPath" class="overFlow">{{scope.row.classPath}}</div>
        </template>
        </el-table-column>
            <el-table-column align="center" label="JSON参数" width="110px">
        <template slot-scope="scope">
          <div :title="scope.row.jobData" class="overFlow">{{scope.row.jobData}}</div>
        </template>
        </el-table-column>
            <el-table-column align="center" label="创建人姓名" width="110px">
        <template slot-scope="scope">
          <div :title="scope.row.createName" class="overFlow">{{scope.row.createName}}</div>
        </template>
        </el-table-column>
        <el-table-column align="center" label="创建时间">
          <template slot-scope="scope">
          <div :title="scope.row.createTime==null?'':new Date(scope.row.createTime).toLocaleString()" class="overFlow">{{scope.row.createTime==null?'':new Date(scope.row.createTime).toLocaleString()}}</div>
        </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="修改人姓名"  width="110px">
          <template slot-scope="scope">
            <div :title="scope.row.updateName" class="overFlow">{{scope.row.updateName}}</div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="修改时间">
          <template slot-scope="scope">
            <div :title="scope.row.updateTime==null?'':new Date(scope.row.updateTime).toLocaleString()" class="overFlow">{{scope.row.updateTime==null?'':new Date(scope.row.updateTime).toLocaleString()}}</div>
          </template>
        </el-table-column> -->
       <el-table-column label="操作" width="248x" fixed="right" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="showEditDialog(scope.$index, scope.row)">查看</el-button>
          <el-button size="mini" @click="showRecord(scope.$index, scope.row)">执行记录</el-button>
          <el-button size="mini" type="warning" @click="doClose(scope.$index, scope.row)">暂停</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-card style="text-align:right;">
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="currentPage"
        :page-sizes="[ 10, 20,30, 40]"
        :page-size="pageSize"
        background
        layout="total, sizes, prev, pager, next"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- --------------------------------------------------------添加弹出框 -->
    <el-dialog title="新增管理" width="500px" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form :model="form" ref="addForm" :rules="rules">
                	<el-form-item label="调度任务名" :label-width="formLabelWidth" prop="jobName">
                 	<el-input v-model="form.jobName" autocomplete="off" placeholder="请输入调度任务名，必须确保唯一"></el-input>
	             </el-form-item>
                	<el-form-item label="调度任务描述" :label-width="formLabelWidth" prop="jobDesc">
                 	<el-input v-model="form.jobDesc" autocomplete="off"></el-input>
	             </el-form-item>
                	<el-form-item label="cron表达式" :label-width="formLabelWidth" prop="jobCron">
                 	<el-input v-model="form.jobCron" autocomplete="off" @focus="doFocus"></el-input>
	             </el-form-item>
                	<el-form-item label="调度任务类型" :label-width="formLabelWidth" prop="jobType">
                   <el-select v-model="form.jobType" placeholder="请选择">
                      <el-option
                        v-for="item in jobTypes"
                        :key="item.code"
                        :label="item.label"
                        :value="item.code">
                      </el-option>
                     </el-select> 
	             </el-form-item>
               
                	<el-form-item label="任务来源ID" :label-width="formLabelWidth" prop="sourceId">
                 	<el-input v-model="form.sourceId" autocomplete="off"></el-input>
	             </el-form-item>
              <!-- <el-form-item label="调度任务状态" :label-width="formLabelWidth" prop="jobState">
                 	<el-input v-model="form.jobState" autocomplete="off"></el-input>
	             </el-form-item> -->
                	<el-form-item label="JSON参数" :label-width="formLabelWidth" prop="jobData">
                 	<el-input v-model="form.jobData" autocomplete="off" placeholder="例如：{'title': '消息标题', 'content': '消息内容'}"></el-input>
	             </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- --------------------------------------------------------编辑弹出框 -->
    <el-dialog title="查看信息" :visible.sync="dialogEditVisible" :close-on-click-modal="false">
      <el-form :model="editForm" ref="editForm" :rules="rules">
        <el-form-item label="调度任务名" :label-width="formLabelWidth" prop="jobName">
          <el-input v-model="editForm.jobName" autocomplete="off"></el-input>
        </el-form-item>
          <el-form-item label="调度任务描述" :label-width="formLabelWidth" prop="jobDesc">
          <el-input v-model="editForm.jobDesc" autocomplete="off"></el-input>
        </el-form-item>
          <el-form-item label="cron表达式" :label-width="formLabelWidth" prop="jobCron">
          <el-input v-model="editForm.jobCron" autocomplete="off"></el-input>
        </el-form-item>
          <el-form-item label="调度任务类型" :label-width="formLabelWidth" prop="jobType">
          <el-input v-model="editForm.jobType" autocomplete="off"></el-input>
        </el-form-item>
          <el-form-item label="任务来源ID" :label-width="formLabelWidth" prop="sourceId">
          <el-input v-model="editForm.sourceId" autocomplete="off"></el-input>
        </el-form-item>
          <el-form-item label="调度任务状态" :label-width="formLabelWidth" prop="jobState">
          <el-input v-model="editForm.jobState" autocomplete="off"></el-input>
        </el-form-item>
          <el-form-item label="JOB类路径" :label-width="formLabelWidth" prop="classPath">
          <el-input v-model="editForm.classPath" autocomplete="off"></el-input>
        </el-form-item>
          <el-form-item label="JSON参数" :label-width="formLabelWidth" prop="jobData">
          <el-input v-model="editForm.jobData" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <!-- <el-button @click="dialogEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEdit()">确 定</el-button> -->
      </div>
    </el-dialog>

    <!-- -------------------------------------------------------配置corn表达式---------- -->
    <el-dialog title="配置CRON表达式" :visible.sync="dialogCrontVisible" width="800px" :close-on-click-modal="false">
      <cronExpression :that.sync="that"></cronExpression>
    </el-dialog>

    <!-- -------------------------------------------------------执行计划--------- -->
    <el-dialog title="执行记录" :visible.sync="dialogRecordVisible" :close-on-click-modal="false">
      <jobRecord :that.sync="that"></jobRecord>
    </el-dialog>
  </div>
</template>

<script>
import cronExpression from "./components/cron.vue";
import jobRecord from "./jobRecord.vue";
import {
  getLabelByCode
} from "./components/function";
export default {
  components: {cronExpression, jobRecord},
  data() {
    return {
      jobStates: [{'code': '1', 'label': '已启用'},
      {'code': '2', 'label': '已暂停'},{'code': '3', 'label': '已删除'},{'code': '4', 'label': '已失效'}],
      dialogRecordVisible: false,
      jobId: '',
      jobTypes: [{"code": "1", "label": "日历提醒事件"}],
      that: this,
      user: {},
      // cron配置dialog
      dialogCrontVisible: false,
      dialogFormVisible: false,
      dialogEditVisible: false,
      offForm: true, //是否折叠
      folding: false, //表单是否超过一行
      formLabelWidth: "100px",
      tableData: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      ruleForm: {
        jobName:'',
        jobDesc:'',
        jobCron:'',
        jobType:'',
        sourceId:'',
        jobState:'',
        classPath:'',
        jobData:'',
        groupName:'',
        createId:'',
        createName:'',
        updateId:'',
        updateName:'',
      },
      style: {},
      rules: {
       
      },
      form: {
        jobName:'',
        jobDesc:'',
        jobCron:'',
        jobType:'',
        sourceId:'',
        jobState:'',
        classPath:'',
        jobData:'',
        groupName:'',
        createId:'',
        createName:'',
        updateId:'',
        updateName:'',
      },
      editForm: {
        id:'',
        jobName:'',
        jobDesc:'',
        jobCron:'',
        jobType:'',
        sourceId:'',
        jobState:'',
        classPath:'',
        jobData:'',
        groupName:'',
        createId:'',
        createName:'',
        updateId:'',
        updateName:'',
      }
    };
  },

  mounted() {
    let user = JSON.parse(sessionStorage.getItem("user"));
    this.user = user;
    this.init();
    let formHeight = this.$refs.ruleForm.$el.clientHeight;
    if (formHeight > 62) {
      this.folding = true;
      this.style = {
        height: "62px",
        overflow: "hidden"
      };
    } else {
      this.folding = false;
      this.style = {
        height: "auto",
        overflow: "hidden"
      };
    }
  },
  computed: {
  },
  watch: {
    offForm: function(v) {
      if (v) {
        this.style = {
          height: "62px",
          overflow: "hidden"
        };
      } else {
        this.style = {
          height: "auto",
          overflow: "hidden"
        };
      }
    }
  },
  methods: {
    getLabelByCode(code, enums) {
      return getLabelByCode(code, enums);
    },
    doFocus() {
      this.dialogCrontVisible = true;
    },
    sizeChange(n) {
      this.pageSize = n;
      this.init();
    },
    currentChange(n) {
      this.currentPage = n;
      this.init();
    },
    init() {
      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        ...this.ruleForm
      };
      this.$api
        .formPost("/api/mess/job/doGetList", params)
        .then(res => {
          if (res.retHead.code == "0000000") {
            this.tableData = res.retBody.list;
            this.total = res.retBody.total;
            this.currentPage = res.retBody.pageNum;
          }
        });
    },
    showDialog() {
      this.dialogFormVisible = true;
      this.form ={};
    },
    showRecord(index, row) {
      this.jobId = row.id;
      this.dialogRecordVisible = true;
    },
    showEditDialog(index, row) {
      this.editForm.id=row.id;
      this.editForm.jobName=row.jobName;
      this.editForm.jobDesc=row.jobDesc;
      this.editForm.jobCron=row.jobCron;
      this.editForm.jobType=this.getLabelByCode(row.jobType, this.jobTypes);
      this.editForm.sourceId=row.sourceId;
      this.editForm.jobState=getLabelByCode(row.jobState, this.jobStates);
      this.editForm.classPath=row.classPath;
      this.editForm.jobData=row.jobData;     
      this.dialogEditVisible = true;
    },
    handleAdd() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          let params = this.form;

          let jobCron = params.jobCron;
          if (!jobCron) {
            this.$message({
                    message: "Cron表达式不存在！",
                    type: "error"
                  });
          } else {
            
            params.createId = this.user.id;
            params.createName = this.user.realName;
            this.$api
              .formPost("/api/mess/job/doSave", params)
              .then(res => {
                if (res.retHead.code == "0000000") {
                  this.$message({
                    message: "添加成功！",
                    type: "success"
                  });
                  this.dialogFormVisible = false;
                  this.init();
                } else {
                  this.$message({
                    message: res.retHead.message,
                    type: "error"
                  });
                }
             });
          }
          
        }
      });
    },
    handleEdit() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          let params = this.editForm;
          params.updateId = this.user.id;
          params.updateName = this.user.realName;
          this.$api
            .formPost("/api/mess/job/doModById", params)
            .then(res => {
              if (res.retHead.code == "0000000") {
                this.$message.success("执行成功！");
                this.init();
                this.dialogEditVisible = false;
              } else {
                this.$message.error("执行失败！");
              }
            });
        }
      });
    },

    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$api
            .formPost("/api/mess/job/doRmById", {
              id: row.id
            })
            .then(res => {
              if (res.retHead.code == "0000000") {
                this.$message({
                  message: "操作成功！",
                  type: "success"
                });
                this.init();
              } else {
                this.$message({
                  message: "操作失败！",
                  type: "error"
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    submitForm(formName) {
      this.init();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    doClose(index, row) {
      this.$confirm("此操作将暂停该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$api
            .formPost("/api/mess/job/doStop", {
              id: row.id
            })
            .then(res => {
              if (res.retHead.code == "0000000") {
                this.$message({
                  message: "操作成功！",
                  type: "success"
                });
                this.init();
              } else {
                this.$message({
                  message: "操作失败！",
                  type: "error"
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
  },
  created() {}
};
</script>
<style lang='scss' scoped>
.btns {
  text-align: right;
}
.icon-arrow {
  position: absolute;
  bottom: 5px;
  left: 0;
  width: 100%;
  height: 1px;
  background-color: #f1f1f1;
  i {
    background-color: #fff;
    position: absolute;
    color: #999;
    bottom: -7px;
    margin-left: 50%;
    transform: translateX(-50%);
    cursor: pointer;
  }
}

.overFlow {
  max-height: 40px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>